/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: SuperToast.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.baselib.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import io.bhex.baselib.R;

import io.bhex.baselib.core.CApplication;

/**

 * 用于解决自带toast因为权限问题导致无法显示的情况
 * ================================================
 */
public class SuperToast {
    public static final int LENGTH_LONG = 3500;
    public static final int LENGTH_SHORT = 2000;

    private int duration;

    private CharSequence text;

    private View mToastView;


    private WindowManager mWindowManager;

    private Handler mHandler;

    private SuperToast(Activity context) {
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        mHandler = new Handler();
    }

    private LayoutParams createLayoutParams() {
        LayoutParams params = new LayoutParams();

        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;

        params.format = PixelFormat.TRANSLUCENT;

        params.windowAnimations = R.style.ToastAnimation;
        //params.type = WindowManager.LayoutParams.TYPE_TOAST;

        params.setTitle(SuperToast.class.getSimpleName());

        params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

        params.packageName = CApplication.getInstance().getPackageName();
        params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER;

        params.horizontalWeight = 1.0f;

        params.x = 0;
        params.y = PixelUtils.dp2px(50);

        return params;
    }

    private View createToastView() {
        LayoutInflater inflater = (LayoutInflater) CApplication.getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(R.layout.view_supertoast_layout, null);
    }

    public static SuperToast makeText(Activity context, CharSequence text, int duration) {
        SuperToast toast = new SuperToast(context);

        toast.text = text;
        toast.duration = duration;

        return toast;
    }

    public void show() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    mToastView = createToastView();

                    if (mToastView.getParent() != null) {
                        mWindowManager.removeViewImmediate(mToastView);
                    }

                    ((TextView) mToastView.findViewById(R.id.message)).setText(text);

                    mWindowManager.addView(mToastView, createLayoutParams());
                }
                catch (Exception e){

                }
            }
        });
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                cancel();
            }
        }, duration);
    }

    public void cancel() {
        if (mToastView != null && mToastView.getParent() != null) {
            mWindowManager.removeViewImmediate(mToastView);
        }
    }
}
